// Light CMS 菜单配置
// 直接在前端定义菜单，不需要从数据库查询

export interface MenuItem {
  id: string
  title: string
  icon: string
  path?: string
  children?: MenuItem[]
  permission?: string
}

export const menuConfig: MenuItem[] = [
  {
    id: 'dashboard',
    title: '首页',
    icon: 'House',
    path: '/dashboard'
  },
  {
    id: 'content',
    title: '内容管理',
    icon: 'Document',
    children: [
      {
        id: 'site',
        title: '站点管理',
        icon: 'Globe',
        path: '/system/site',
        permission: 'cms:site:view'
      },
      {
        id: 'channel',
        title: '栏目管理',
        icon: 'Menu',
        path: '/system/channel',
        permission: 'cms:channel:view'
      },
      {
        id: 'article',
        title: '文章管理',
        icon: 'Edit',
        path: '/system/article',
        permission: 'cms:article:view'
      },
      {
        id: 'attachment',
        title: '附件管理',
        icon: 'Paperclip',
        path: '/system/attachment',
        permission: 'cms:attachment:view'
      },
      {
        id: 'block',
        title: '区块管理',
        icon: 'Grid',
        path: '/content/block',
        permission: 'cms:block:view'
      }
    ]
  },
  {
    id: 'template',
    title: '模板管理',
    icon: 'Files',
    children: [
      {
        id: 'theme',
        title: '主题管理',
        icon: 'Brush',
        path: '/system/theme',
        permission: 'cms:theme:view'
      },
      {
        id: 'template-file',
        title: '模板文件管理',
        icon: 'Document',
        path: '/system/template-file',
        permission: 'cms:template-file:view'
      },
      {
        id: 'generate',
        title: '静态化生成',
        icon: 'Lightning',
        path: '/system/generate',
        permission: 'cms:generate:view'
      }
    ]
  },
  {
    id: 'model',
    title: '模型管理',
    icon: 'Grid',
    children: [
      {
        id: 'model-main',
        title: '内容模型',
        icon: 'Grid',
        path: '/system/model',
        permission: 'cms:model:view'
      }
    ]
  },
  {
    id: 'system',
    title: '系统管理',
    icon: 'Setting',
    children: [
      {
        id: 'user',
        title: '用户管理',
        icon: 'User',
        path: '/system/user',
        permission: 'sys:user:view'
      },
      {
        id: 'role',
        title: '角色管理',
        icon: 'UserFilled',
        path: '/system/role',
        permission: 'sys:role:view'
      },
      {
        id: 'menu',
        title: '菜单管理',
        icon: 'Menu',
        path: '/system/menu',
        permission: 'sys:menu:view'
      },
      {
        id: 'dept',
        title: '部门管理',
        icon: 'OfficeBuilding',
        path: '/system/dept',
        permission: 'sys:dept:view'
      },
      {
        id: 'dict',
        title: '字典管理',
        icon: 'Collection',
        path: '/system/dict',
        permission: 'sys:dict:view'
      },
      {
        id: 'params',
        title: '参数管理',
        icon: 'Tools',
        path: '/system/params',
        permission: 'sys:params:view'
      },
      {
        id: 'log',
        title: '日志管理',
        icon: 'Document',
        path: '/system/log',
        permission: 'sys:log:view'
      }
    ]
  }
]

// 菜单权限检查函数
export function hasPermission(permission: string, userPermissions: string[]): boolean {
  if (!permission) return true
  return userPermissions.includes(permission) || userPermissions.includes('*:*:*')
}

// 过滤菜单（根据权限）
export function filterMenuByPermission(menus: MenuItem[], userPermissions: string[]): MenuItem[] {
  return menus.filter(menu => {
    // 检查当前菜单权限
    if (menu.permission && !hasPermission(menu.permission, userPermissions)) {
      return false
    }

    // 如果有子菜单，递归过滤
    if (menu.children && menu.children.length > 0) {
      menu.children = filterMenuByPermission(menu.children, userPermissions)
      // 如果过滤后没有子菜单，则隐藏父菜单
      return menu.children.length > 0
    }

    return true
  })
}

// 获取用户菜单
export function getUserMenus(userPermissions: string[] = ['*:*:*']): MenuItem[] {
  return filterMenuByPermission(JSON.parse(JSON.stringify(menuConfig)), userPermissions)
}

// 扁平化菜单（用于路由匹配）
export function flattenMenu(menus: MenuItem[]): MenuItem[] {
  const result: MenuItem[] = []
  function flatten(items: MenuItem[]) {
    items.forEach(item => {
      if (item.path) {
        result.push(item)
      }
      if (item.children) {
        flatten(item.children)
      }
    })
  }

  flatten(menus)
  return result
}

// 根据路径查找菜单项
export function findMenuByPath(menus: MenuItem[], path: string): MenuItem | null {
  for (const menu of menus) {
    if (menu.path === path) {
      return menu
    }
    if (menu.children) {
      const found = findMenuByPath(menu.children, path)
      if (found) return found
    }
  }
  return null
}

// 获取面包屑导航
export function getBreadcrumb(menus: MenuItem[], path: string): MenuItem[] {
  const breadcrumb: MenuItem[] = []

  function findPath(items: MenuItem[], targetPath: string, currentPath: MenuItem[] = []): boolean {
    for (const item of items) {
      const newPath = [...currentPath, item]

      if (item.path === targetPath) {
        breadcrumb.push(...newPath)
        return true
      }

      if (item.children && findPath(item.children, targetPath, newPath)) {
        return true
      }
    }
    return false
  }

  findPath(menus, path)
  return breadcrumb
}

export default {
  menuConfig,
  hasPermission,
  filterMenuByPermission,
  getUserMenus,
  flattenMenu,
  findMenuByPath,
  getBreadcrumb
}
